import {
  createRouter,
  createWebHashHistory,
  createWebHistory
} from 'vue-router'
import Layout from '@/layouts/index.vue'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'
import { HOME_URL } from '@/config/app'

import appRoutes from './routes'
import createRouteGuard from './guard'

NProgress.configure({
  // NProgress Configuration
  easing: 'ease', // 动画方式
  speed: 500, // 递增进度条的速度
  showSpinner: true, // 是否显示加载ico
  trickleSpeed: 200, // 自动递增间隔
  minimum: 0.3 // 初始化时的最小百分比
})
const router = createRouter({
  // history: createWebHistory(),
  history: createWebHashHistory(),
  routes: [
    {
      path: '/login',
      name: 'login',
      component: () => import('@/views/login.vue'),
      meta: {
        requiresAuth: false,
        title: 'login'
      }
    },
    {
      name: 'root',
      path: '/',
      component: Layout,
      children: appRoutes,
      redirect: HOME_URL,
      meta: {
        title: 'root',
        requiresAuth: true,
        roles: ['*']
      }
    },
    {
      path: '/403',
      name: '403',
      component: () => import('@/views/Error/403.vue'),
      meta: {
        title: 'error403',
        requiresAuth: false
      }
    },
    {
      path: '/404',
      name: '404',
      component: () => import('@/views/Error/404.vue'),
      meta: {
        title: 'error404',
        requiresAuth: false
      }
    },
    {
      path: '/500',
      name: '500',
      component: () => import('@/views/Error/500.vue'),
      meta: {
        title: 'error500',
        requiresAuth: false
      }
    },
    {
      path: '/:pathMatch(.*)*',
      name: 'notFound',
      component: () => import('@/views/Error/404.vue'),
      meta: {
        title: 'NotFound',
        requiresAuth: false
      }
    }
  ],
  scrollBehavior() {
    return { top: 0 }
  }
})

createRouteGuard(router)

export default router
